# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: xguo

from coin.exchange.base.kr_rest.native_public_client_base import (
    NativePublicClientBase,)
from coin.exchange.okex_v3.kr_rest.util import convert_to_iso_datetime_str


class OkexNativePublicClient(NativePublicClientBase):
  def __init__(self, timeout=10):
    super().__init__(url="https://www.okex.com", timeout=timeout)
    self._valid_kline_period = [
        60, 180, 300, 900, 1800, 3600, 7200, 14400, 21600, 43200, 86400, 604800
    ]

  def check_kline_period(self, kline_period):
    assert kline_period in self._valid_kline_period, \
      'Invalid kline period!'

  def get_order_book(self, instrument_id, size=None, depth=None):
    path = 'api/spot/v3/instruments/{instrument_id}/book'
    path = path.format(instrument_id=instrument_id)
    params = {
        'size': size,
        'depth': depth,
    }
    update = self._get_as_json(path, params)
    return update

  def get_exchange_ticker(self):
    path = 'api/spot/v3/instruments/ticker'
    update = self._get_as_json(path)
    return update

  def get_ticker(self, instrument_id):
    path = 'api/spot/v3/instruments/{instrument_id}/ticker'
    path = path.format(instrument_id=instrument_id)
    update = self._get_as_json(path)
    return update

  def get_history_kline(self, instrument_id, kline_period, start=None, end=None):
    self.check_kline_period(kline_period)
    path = "api/spot/v3/instruments/{instrument_id}/candles"
    path = path.format(instrument_id=instrument_id)
    params = {
        'granularity': kline_period,
    }
    if start:
      params['start'] = convert_to_iso_datetime_str(start)
    if end:
      params['end'] = convert_to_iso_datetime_str(end)
    return self._get_as_json(path, params)

  def queryAll(self):
    pass
